package rmiserver;

import java.util.List;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.RemoteException;

import opencsv.CSVReader;
import opencsv.CSVWriter;
import util.AppFeatures;
import util.Employee;
import util.Institution;

public class AppFeaturesImpl implements AppFeatures {
	private FileWriter fileWriter = null;
	private FileReader fileReader = null;
	private CSVReader reader = null;
	private CSVWriter writer = null;

	private List<String[]> list = null;

	protected AppFeaturesImpl() throws RemoteException {
		super();
	}

	private boolean verifyInstitutionFile(String institutionName) {

		String filePath = "./" + institutionName + ".csv";

		File file = new File(filePath);

		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {

			}
			return false;
		}
		return true;
	}

	@Override
	public int addEmployee(Employee employee) {
		String institution = employee.getInstitution();
		verifyInstitutionFile(institution);
		String filePath = "./" + institution + ".csv";
		boolean readingBlockEntered = false;
		try {
			fileReader = new FileReader(filePath);
			reader = new CSVReader(fileReader, ',', '\b');
			list = reader.readAll();
			reader.close();
			fileReader.close();
			readingBlockEntered = true;
		} catch (IOException e) {
			return -1;
		}

		try {
			list.add(employee.toStringList());

			fileWriter = new FileWriter(filePath);
			writer = new CSVWriter(fileWriter, ',', '\b');
			writer.writeAll(list);
			writer.close();
			fileWriter.close();

			if (readingBlockEntered) {
				return 1;
			}

		} catch (IOException e1) {
			return -1;
		}
		return 0;
	}

	@Override
	public int updateEmployee(Employee employee) {
		String institution = employee.getInstitution();
		String name = employee.getFirstName();

		verifyInstitutionFile(institution);
		String filePath = "./" + institution + ".csv";
		try {
			fileReader = new FileReader(filePath);
			reader = new CSVReader(fileReader, ',', '\b');
			list = reader.readAll();
			reader.close();
			fileReader.close();

			int i = 0;
			int index = -1;
			for (String[] element : list) {
				if (element[0].equals(name)) {
					index = i;
					break;
				}
				i++;
			}

			if (index >= 0) {
				list.set(index, employee.toStringList());
				try {
					fileWriter = new FileWriter(filePath);
					writer = new CSVWriter(fileWriter, ',', '\b');
					writer.writeAll(list);
					writer.close();
					fileWriter.close();
					return 1;
				} catch (IOException e1) {
					return -1;
				}

			} else {
				return -1;
			}

		} catch (IOException e) {
			return -1;
		}
	}

	@Override
	public int deleteEmployee(String name, String institution) {
		verifyInstitutionFile(institution);
		String filePath = "./" + institution + ".csv";
		try {
			fileReader = new FileReader(filePath);
			reader = new CSVReader(fileReader, ',', '\b');
			list = reader.readAll();
			reader.close();
			fileReader.close();

			int i = 0;
			int index = -1;
			for (String[] element : list) {
				if (element[0].equals(name)) {
					index = i;
					break;
				}
				i++;
			}

			if (index >= 0) {
				list.remove(index);
				try {
					fileWriter = new FileWriter(filePath);
					writer = new CSVWriter(fileWriter, ',', '\b');
					writer.writeAll(list);
					writer.close();
					fileWriter.close();
					return 1;
				} catch (IOException e1) {
					return -1;
				}

			} else {
				return -1;
			}

		} catch (IOException e) {
			return -1;
		}
	}

	@Override
	public Employee searchEmployee(String name, String institution) {
		verifyInstitutionFile(institution);
		String filePath = "./" + institution + ".csv";
		try {
			fileReader = new FileReader(filePath);
			reader = new CSVReader(fileReader, ',', '\b');
			list = reader.readAll();
			reader.close();
			fileReader.close();

			for (String[] element : list) {
				if (element[0].equals(name)) {
					Employee emp = new Employee(element[0], element[1],
							element[2], element[3], institution);
					return emp;
				}
			}

		} catch (IOException e) {

		}
		return null;
	}

	@Override
	public Institution getInstitution(String institutionName) {
		verifyInstitutionFile(institutionName);
		String filePath = "./" + institutionName + ".csv";
		try {
			fileReader = new FileReader(filePath);
			reader = new CSVReader(fileReader, ',', '\b');
			list = reader.readAll();
			reader.close();
			fileReader.close();

			Institution inst = new Institution(list, institutionName);
			return inst;
		} catch (IOException e) {

		}
		return null;
	}
}
